package cn.bkw.ytkapi.controller;

import cn.bkw.ytkapi.utils.helper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 页面跳转请求
 */
@Controller
public class InterceptController {

    private static String LOGIN = "login";

    // 未登录返回false
    private boolean checkOnline(HttpServletRequest req) {
        HttpSession session = req.getSession();
        if (helper.ReadCookieMap(req, "admin_usercookie").toString() != null) {
            return true;
        }
        return false;
    }

    private String interceptLogin(HttpServletRequest req, String tourl) {
        if (!checkOnline(req)) {
            return LOGIN;
        }
        return tourl;
    }

    //跳转到登录页面
    @RequestMapping(value="/")
    public ModelAndView getlogin(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("/login");
        return mv;
    }

    //退出
    @RequestMapping("/loginout")
    public String userExit(HttpServletRequest req,HttpServletResponse resp) {
        HttpSession session = req.getSession();
        session.removeAttribute("login");
        session.removeAttribute("sessionid");
        session.removeAttribute("uid");
        //删除cookie
        helper.DelLoginCookie(resp, req);
        return LOGIN;
    }


    //主页
    @RequestMapping(value = "/index")
    public String index(HttpServletRequest req) {
        String interceptLogin = interceptLogin(req, "/user/userList");
        return interceptLogin;
    }

    //主页
    @RequestMapping(value = "/userRight")
    public String userRight(HttpServletRequest req) {
        String interceptLogin = interceptLogin(req, "/user/userRight");
        return interceptLogin;
    }

    //主页
    @RequestMapping(value = "/userlist")
    public String userList(HttpServletRequest req) {
        String interceptLogin = interceptLogin(req, "/user/index");
        return interceptLogin;
    }

    //客户信息管理
    @RequestMapping(value = "/customerIndex")
    public String customerIndex(HttpServletRequest req) {
        String interceptLogin = interceptLogin(req, "/customer/customerIndex");
        return interceptLogin;
    }
    //修改客户信息
    @RequestMapping(value = "/changeCustomerDetails")
    public String changeCustomerDetails(HttpServletRequest req) {
        String interceptLogin = interceptLogin(req, "/customer/changeCustomerDetails");
        return interceptLogin;
    }
    //新增客户信息
    @RequestMapping(value = "/addCustomer")
    public String addCustomer(HttpServletRequest req) {
        String interceptLogin = interceptLogin(req, "/customer/addCustomer");
        return interceptLogin;
    }
    //实名认证管理
    @RequestMapping(value = "/realName")
    public String realName(HttpServletRequest req) {
        String interceptLogin = interceptLogin(req, "/customer/realName");
        return interceptLogin;
    }
    //实名认证详情
    @RequestMapping(value = "/authenticationDetails")
    public String authenticationDetails(HttpServletRequest req) {
        String interceptLogin = interceptLogin(req, "/customer/authenticationDetails");
        return interceptLogin;
    }
}
